//
//  FControl.m
//  test
//
//  Created by 袁飞 on 12-7-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FButton.h"
#import "TBXML.h"
#import "FUtil.h"
#import "ButtonHandle.h"
/***
 <button>
 <positon>0,0</position>
 <name>b1</name>
 <normal>btn_4_clicked.png</normal>
 <highlight>btn_4_clicked.png</highlight>
 <disabled>btn_4_clicked.png</disabled>
 </button>
 ****/


@implementation FButton

-(id)init{
  [super init];
  return self;
}

+(FButton*)FButtonWithName:(NSString*)name Normal:(NSString*)normal Highlight:(NSString*)highlight Disabled:(NSString*)disabled Scaled:(float)sc{
  FButton* b = [[[FButton alloc] initWithWithName:name Normal:normal Highlight:highlight Disabled:disabled Scaled:sc] autorelease];
  return b;
}

+(FButton*)FButtonWithFile:(NSString*)file{
  TBXML *xml = [[TBXML alloc] initWithXMLFile:file];
  if (!xml) {
    NSLog(@"err");
  }
  TBXMLElement *root_element = [xml rootXMLElement];  
  
  TBXMLElement* te = [TBXML childElementNamed:@"positon" parentElement:root_element];
  NSString* value = [TBXML textForElement:te];
  CGPoint positon = [FUtil string2Point:value];
  
  te = [TBXML childElementNamed:@"name" parentElement:root_element];
  NSString *name = [TBXML textForElement:te];
  
  
  NSString *normal, *highlight, *disabled;
  te = [TBXML childElementNamed:@"normal" parentElement:root_element];
  if (te) {
    normal = [TBXML textForElement:te];
  }else{
    return nil;
  }
  
  te = [TBXML childElementNamed:@"highlight" parentElement:root_element];
  if (te) {
    highlight = [TBXML textForElement:te];
  }else{
    highlight = normal;
  }
  
  te = [TBXML childElementNamed:@"disabled" parentElement:root_element];
  if (te) {
    disabled = [TBXML textForElement:te];
  }else{
    disabled = normal;
  }
  
  FButton *b = [FButton FButtonWithName:name Normal:normal Highlight:highlight Disabled:disabled Scaled:1.0f];
  [b setPosition:positon];
  return b;
}

+(FButton*)FButtonWithXml:(NSString*)data{
  TBXML *xml = [[TBXML alloc] initWithXMLString:data];
  if (!xml) {
    NSLog(@"err");
  }
  TBXMLElement *root_element = [xml rootXMLElement];  
  
  TBXMLElement* te = [TBXML childElementNamed:@"positon" parentElement:root_element];
  NSString* value = [TBXML textForElement:te];
  CGPoint positon = [FUtil string2Point:value];
  
  te = [TBXML childElementNamed:@"name" parentElement:root_element];
  NSString *name = [TBXML textForElement:te];
  
  te = [TBXML childElementNamed:@"normal" parentElement:root_element];
  NSString *normal = [TBXML textForElement:te];
  
  te = [TBXML childElementNamed:@"highlight" parentElement:root_element];
  NSString *highlight = [TBXML textForElement:te];
  
  te = [TBXML childElementNamed:@"disabled" parentElement:root_element];
  NSString *disabled = [TBXML textForElement:te];
  
  FButton *b = [FButton FButtonWithName:name Normal:normal Highlight:highlight Disabled:disabled Scaled:1.0f];
  [b setPosition:positon];
  return b;
}

-(id)initWithWithName:(NSString*)name Normal:(NSString*)normal Highlight:(NSString*)highlight Disabled:(NSString*)disabled Scaled:(float)sc{
  [super init];
  normal_sprite_    = [CCSprite spriteWithSpriteFrameName:normal];
  [normal_sprite_ retain];
  highlight_sprite_ = [CCSprite spriteWithSpriteFrameName:highlight];
  [highlight_sprite_ retain];
  disabled_sprite_  = [CCSprite spriteWithSpriteFrameName:disabled];
  [disabled_sprite_ retain];
  name_ = name;
  scaled_ = sc;
  [self setCurentSprite:normal_sprite_];
  
  //CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:12];

  title_ = [CCLabelTTF labelWithString:name_ fontName:@"Marker Felt" fontSize:12];
  [self addChild:title_ z:1];
  return self;
}

-(void)setCurentSprite:(CCSprite*)sp{
  if(current_sprite_ && current_sprite_!=sp){
    [self removeChild:current_sprite_ cleanup:false];  
  }
  
  if (!current_sprite_ || current_sprite_!=sp) {
    current_sprite_ = sp;
    [self addChild:sp];
  }
}

-(void)onTouchDown:(CGPoint)point{
  [self setCurentSprite:highlight_sprite_];
  if(scaled_ > 1){
    CCScaleBy *action = [CCScaleBy actionWithDuration:0.05 scale:scaled_]; 
    [current_sprite_ runAction:action];
  }
  //NSLog(@"down %@", name_);
}

-(void)onTouchUp:(CGPoint)point{
  if(scaled_ > 1){
    CCScaleBy *action = [CCScaleBy actionWithDuration:0.05 scale:1]; 
    //[current_sprite_ runAction:action];
    CCCallFunc* ca = [CCCallFunc actionWithTarget:self selector:@selector(onActionEnd)];
    [current_sprite_ runAction:[CCSequence actions:action,ca,nil]];
  } 
  [self setCurentSprite:normal_sprite_];
  //NSLog(@"up %@", name_);
  //NSLog(@"%@ %f %f %f %f", name_, point_begin_.x, point_begin_.y, point.x, point.y);

}

-(void)onTouchCancel:(CGPoint)point{
  if(scaled_ > 1){
    CCScaleBy *action = [CCScaleBy actionWithDuration:0.05 scale:1]; 
    [current_sprite_ runAction:action];
  } 
  [self setCurentSprite:normal_sprite_];
}

-(void)onClick:(CGPoint)point{
  //CCLayer
  //NSLog(@"@@@@@@@ %@", name_);
  [[ButtonHandle sharedButtonHandle] onHandle:name_];
}


-(void)onActionEnd{
  //NSLog(@"onActionEnd");
  //SEL sel = NSSelectorFromString(@"handleButton:");
  //[[self parent] performSelector:sel withObject:self];
}

-(CGRect)getControlRect{
  CGRect rect = [normal_sprite_ textureRect];
  CGPoint ap = [normal_sprite_ anchorPoint];
  float x = -rect.size.width*ap.x;
  float y = -rect.size.height*ap.y;
  //NSLog(@"size %f, %f %f, %f",x, y, rect.size.width, rect.size.height);
  return CGRectMake(x, y, rect.size.width, rect.size.height);
}

-(NSString*)getName{
  return name_;
}

@end
